package com.liupengyuan.framework.util;

import java.io.PrintWriter;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import com.liupengyuan.framework.web.query.IGenericPage;

public class JSONOuter {
	static JsonConfig jsonConfig = new JsonConfig();
	/**
	 * 输出并返回分页查询的JSON字符串
	 * @param printWriter
	 * @param iPage
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static String writePage(PrintWriter printWriter,IGenericPage iPage){
		JSONObject resultMap = new JSONObject();
		resultMap.put("page", iPage.getPageNo());
		resultMap.put("pageSize", iPage.getPageSize());
		resultMap.put("rows", iPage.getThisPageElements() == null ? Collections.EMPTY_LIST : iPage.getThisPageElements());
		resultMap.put("records", iPage.getTotalCount());
		resultMap.put("total", iPage.getLastPageNo());
	
		resultMap.put("success", true);
    	String json = resultMap.toString();
		printWriter.print(json);
		return json;
	}
	
	/**
	 * 输出并返回 数组JSON字符串 
	 * @param printWriter
	 * @param list
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static String writeList(PrintWriter printWriter,List list){
		JSONArray jsonArray = JSONArray.fromObject(list);
		String json = jsonArray.toString();
		printWriter.print(json);
		return json;
	}
	/**
	 * 输出并返回JSON字符串 数据库返回的操作行数
	 * @param printWriter
	 * @param count	数据库返回的操作行数
	 * @return
	 * @deprecated
	 */
	public static String writeMsg(PrintWriter printWriter , Long count){
		String msg = "操作成功";
		boolean flag = true;
		if(count < 1){
			flag = false;
			msg="操作失败";
		}
		Map<String,Long> data = new HashMap<String,Long>();
		data.put("count", count);
		return writeMsg(printWriter , msg , data , flag);
	}
	/**
	 * 输出并返回JSON字符串 消息
	 * @param printWriter
	 * @param msg 要输出的成功消息
	 * @return
	 */
	public static String writeSuccessMsg(PrintWriter printWriter , String msg){
		return writeMsg(printWriter ,  msg , null ,  true);
	}
	
	/**
	 * 输出并返回JSON字符串  实体,消息, isSuccess默认为true
	 * @param printWriter
	 * @param obj 实体
	 * @param msg 消息
	 * @return
	 */
	public static String writeBeanMsg(PrintWriter printWriter ,Object obj, String msg){
		return writeMsg(printWriter , msg , obj , true);
	}
	
	/**
	 * 输出并返回JSON字符串  实体, isSuccess默认为true
	 * @param printWriter
	 * @param obj 实体
	 * @return 
	 */
	public static String writeBeanMsg(PrintWriter printWriter ,Object obj){
		return writeMsg(printWriter , null , obj ,  true);
	}
	
	/**
	 * 输出并返回JSON字符串
	 * @param printWriter
	 * @param msg 返回信息
	 * @param obj 实体数据
	 * @param isSuccess 是否成功
	 * @return
	 */
	public static String writeMsg(PrintWriter printWriter , String msg , Object obj , boolean isSuccess){
		JSONObject resultMap = new JSONObject();
		resultMap.put("message", msg);
		resultMap.put("success", isSuccess);
		resultMap.put("data", obj);
    	String json = resultMap.toString();
		printWriter.println(json);
		return json;
	}
	
	/**
	 * 输出返回字符串
	 * @param printWriter
	 * @param msg
	 * @return
	 */
	public static String writeStr(PrintWriter printWriter , String msg){
		printWriter.println(msg);
		return msg;
		
	}
	
	/**
	 * 输出返回TreeNode JSON
	 * @param printWriter
	 * @param treeList List<TreeNode>
	 * @return 
	 */
	public static String writeTreeNode(PrintWriter printWriter , List<TreeNode> treeList){
		JSONArray jsonArray = JSONArray.fromObject(treeList, new JsonConfig());
		String json = jsonArray.toString();
		printWriter.println(json);
		return json;
		
	}
	
	
}
